package cn.itsource.celljava.system.shop.service.impl;

import cn.itsource.celljava.constants.BaseConstants;
import cn.itsource.celljava.system.shop.domain.Shop;
import cn.itsource.celljava.system.shop.domain.ShopRo;
import cn.itsource.celljava.system.shop.mapper.ShopMapper;
import cn.itsource.celljava.system.shop.query.ShopQuery;
import cn.itsource.celljava.system.shop.service.IShopService;
import cn.itsource.celljava.base.service.impl.BaseServiceImpl;
import cn.itsource.celljava.system.employee.domain.Employee;
import cn.itsource.celljava.system.employee.mapper.EmployeeMapper;
import cn.itsource.celljava.utils.BusinessException;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
//    @Autowired
//    private AuthService authService;

    @Override
    public void add(Shop shop) {
        //一堆判断
        if(shop.getName() == null){
            throw new BusinessException("店铺名不能为空");
        }
        if(shop.getAddress() == null){
            throw new BusinessException("地址不能为空");
        }
        if(shop.getTel() == null){
            throw new BusinessException("联系方式不能为空");
        }

//        //百度AI智能认证
//        if(!authService.authentication(shop.getName())){
//            throw new BusinessException("你的店铺名存在违规信息");
//        }


        //校验店铺是否存在
        ShopQuery shopQuery = new ShopQuery();
        shopQuery.setName(shop.getName());
        Long count = shopMapper.findByQueryCount(shopQuery);
        if(count!=0){
            throw new BusinessException("该["+shop.getName()+"]店铺名已存在");
        }
        //补充默认数据
        shop.setRegisterTime(new Date());//注册时间
        shop.setState(BaseConstants.Shop.STATE_WAIT_AUTID);//审核状态

//        shop.setAdminId(?);
        //获取到当前注册的管理员信息
        Employee admin = shop.getAdmin();
        admin.setState(BaseConstants.Employee.STATE_NO_ACTIVE);
        //保存数据库 才能得到 admin_id
        employeeMapper.add(admin);//当我保存  那么员工的id就有了

        shop.setAdminId(admin.getId());//将员工的id设置为管理员
        shopMapper.add(shop);   //当我保存  那么店铺的id就有了

        admin.setShopId(shop.getId());//将店铺的id设置给员工
        employeeMapper.update(admin);//将数据保存进去、
    }

    @Override
    public void update(Shop shop) {
        super.update(shop);
    }

    @Override
    public List<ShopRo> countByDate() {
        return shopMapper.countByDate();
    }

    @Override
    public void activation(Long shopid) {
        //修改店铺的状态
        Shop one = shopMapper.findOne(shopid);
        one.setState(BaseConstants.Shop.STATE_ACTIVE_SUCCESS);
        shopMapper.update(one);
        //得到管理员的id
        Long adminId = one.getAdminId();
        Employee one1 = employeeMapper.findOne(adminId);
        one1.setState(BaseConstants.Employee.STATE_NORMAL);
        employeeMapper.update(one1);
    }
}
